package com.wswy.wzcx.ui.main.community

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import com.wswy.wzcx.R
import com.wswy.wzcx.model.DataCenter
import com.wswy.wzcx.module.base.Func1
import kotlinx.android.synthetic.main.layout_interactive_item.view.*

class InteractiveLayout : LinearLayout {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {


        orientation = LinearLayout.HORIZONTAL

        gravity = Gravity.CENTER_HORIZONTAL

        init(context)

    }


    var onUserNoLoginClick : ( (Func1)->Unit?)?=null

    var replyClickListener : View.OnClickListener?=null

    private fun init(context: Context){

        View.inflate(context, R.layout.layout_interactive_item,this)


    }

    fun hideReview(){
        ll_review.visibility = View.GONE
    }

    override fun onFinishInflate() {
        super.onFinishInflate()

        ll_review.setOnClickListener {
            likeAnim(img_review)
            it.tag = voData
            replyClickListener?.onClick(it)
        }

        ll_like.setOnClickListener {

            if (DataCenter.get().hasLogin()){
                if (!it.isSelected){
                    likeAnim(img_like)
                }
            }else{

                onUserNoLoginClick?.invoke {
                    if (!it.isSelected){
                        likeAnim(img_like)
                    }
                }

            }


        }

    }

    private var voData:InteractiveVO?=null

    fun bindData(vo:InteractiveVO){
        voData = vo
        showNum(vo)

        img_like.isSelected = vo.isLiked()

    }


    private fun showNum(vo: InteractiveVO){
        if (vo.getReviewCount() > 0){
            tv_review.visibility = View.VISIBLE
            tv_review.text = vo.getReviewCount().toString()
        }else{
            tv_review.text = 1.toString()
            tv_review.visibility = View.INVISIBLE
        }

        if (vo.getLikeCount() > 0){
            tv_like.visibility = View.VISIBLE
            tv_like.text = vo.getLikeCount().toString()
        }else{
            tv_like.text = 1.toString()
            tv_like.visibility = View.INVISIBLE
        }
    }

    private fun likeAnim(view: View){

        ObjectAnimator.ofPropertyValuesHolder(view,PropertyValuesHolder.ofFloat(View.SCALE_X,1f,1.3f,1f),
                PropertyValuesHolder.ofFloat(View.SCALE_Y,1f,1.3f,1f)).run {

            var max = 0f

            var once = false

            addUpdateListener {

                (it.animatedValue as? Float)?.run {
                    if (max > 0 && this < max && !once){


                        voData?.run {

                            if (!isLiked()){

                                view.isSelected = !view.isSelected

                                if (view.isSelected){
                                    addLike()
                                }else{
                                    removeLike()
                                }
                                showNum(this)
                            }
                        }

                        once = true
                    }
                    max = this
                }

            }


            start()
        }

    }

}